package com.zzu.team.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzu.team.entity.Result;
import com.zzu.team.entity.ReturnBean;
import com.zzu.team.entity.Tester;
import com.zzu.team.service.ResultService;
import com.zzu.team.service.TesterService;
import com.zzu.team.util.Constants;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 性格测试者(Tester)表控制层
 *
 * @author makejava
 * @since 2021-12-03 09:32:58
 */
@RestController
@RequestMapping("tester")
public class TesterController extends BaseController{
    /**
     * 服务对象
     */
    @Resource
    private TesterService testerService;

    @Resource
    private ResultService resultService;

    // 分页查询 所有用户
    @RequestMapping("selectByPage")
    public ReturnBean selectAll(Integer page, Integer limit, Tester tester) {

        if (page == null){
            page = Constants.page;
            limit=Constants.limit;
        }
        Page<Tester> pageTester = new Page<>(page, limit);

        QueryWrapper<Tester> queryWrapper = new QueryWrapper<>();
        if (Strings.isNotEmpty(tester.getName())){
            queryWrapper.like("name", tester.getName());
        }

        if (Strings.isNotEmpty(tester.getPhone())){
            queryWrapper.like("phone", tester.getPhone());
        }
        Page<Tester> page1 = testerService.page(pageTester, queryWrapper);

        return super.success(page1.getRecords(), page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Tester selectById(@PathVariable Serializable id) {
        Tester tester = testerService.getById(id);
        return tester;
    }


    /**
     * 根据用户名和手机号 登录(查询)
     * @return 返回查询结果
     */
    @PostMapping("selectByNameAndPhone")
    public ReturnBean selectByNameAndPhone(@RequestBody Tester tester, HttpSession session){
        Map<String, Object> map = new HashMap<>();
        // 先根据手机号查询
        map.put("phone", tester.getPhone());
        List<Tester> testers1 = testerService.listByMap(map);
        // 如果手机号存在 testers不为空
        if (testers1.size()>0) {
            // 再根据用户名查询
            map.put("name", tester.getName());
            List<Tester> testers2 = testerService.listByMap(map);
            // 如果此时testers2为空，说明用户名错误
            if (testers2.size()==0) {
                return fail(tester,"用户名错误");
            } else {    // 否则查询成功, 说明该用户之前登录过
                // 判断该用户是否测试成功，如果没测试成功则没有提交，result表中没有他的数据
                // 查询result表中有没该用户对应的数据
                Tester tester1 = testers2.get(0);

                List<Result> res = resultService.selectByTesterId(tester1.getId());
                if (res.size()>0)  {   //说明result表中有该用户对应的数据
                    // 返回该用户的测试结果 跳转到测试结果页面
                    System.out.println("跳转到测试结果页面");
                    return fail(tester1, "查看结果");
                }else{
                    //result表中没有该用户的测试数据
                    // 跳转到测试问题页面
                    System.out.println("跳转到测试问题页面进行测试");
                    return fail(tester1,"重新测试");
                }


            }
        }
        // 如果手机号不存在, 添加该用户 成功添加
        else {
            tester.setCreateTime(new Date());
            return insert(tester);
        }
    }

    /**
     * 新增数据
     *
     * @param tester 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ReturnBean insert(@RequestBody Tester tester) {
        boolean save = testerService.save(tester);
        if (save) {
            return super.success(tester);
        }
        return fail(tester);
    }

    /**
     * 修改数据
     *
     * @param tester 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Tester tester) {
        boolean update = testerService.updateById(tester);
        if (update) {
            return super.success(tester);
        }
        return fail(tester);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @RequestMapping("deleteByIdList")
    public ReturnBean delete(@RequestParam(value = "idList[]", required = false) List<Long> idList) {
        System.out.println(idList.size());
        boolean delete = testerService.removeByIds(idList);
        if (delete) return success(idList);
        return fail(idList);
    }

    @DeleteMapping("deleteById")
    public ReturnBean delete(@RequestParam("id")Integer id) {
        boolean delete = testerService.removeById(id);
        if (delete) return success("删除成功");
        return fail("未删除成功");
    }
}
